package com.sky.controller.admin;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品管理")
@RequiredArgsConstructor
public class DishController {
    private final DishService dishService;
    @GetMapping("page")
    @ApiOperation("分页查询")
    public Result selectDishPage(DishPageQueryDTO dto) {
        PageResult result =dishService.selecetDishPage(dto);
        return Result.success(result);
    }

    @PostMapping
    @ApiIgnore("新增菜品")
    public Result add(@RequestBody DishDTO dto){
        dishService.add(dto);
        return Result.success();

    }

    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dto){
       dishService.updateDish(dto);
       return Result.success();
    }
    @GetMapping("/{id}")
    @ApiOperation("根据id查询")
    public Result<DishVO> getById(@PathVariable Long id){
       DishVO dishVO = dishService.selectDishById(id);
       return Result.success(dishVO);
    }

    @GetMapping("list")
    @ApiOperation("根据分类id查询菜品")
    public Result list(Long categoryId) {
       List<DishVO> list = dishService.selectByCategoryId(categoryId);
        return Result.success(list);
    }
    @ApiOperation("批量删除菜品")
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids){
        dishService.removeDishByIds(ids);
        return Result.success();
    }

    @ApiOperation("起售、停售菜品")
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status,Long id){
        dishService.update(Wrappers.<Dish>lambdaUpdate().eq(Dish::getId,id).set(Dish::getStatus,status));
        return Result.success();
    }
}
